package vcsn.entities;

import vcsn.Config;

public class AddCastingOperation extends ConflictOperation {

	private String castToType;
	private String originIdentifier;
	private String refinedIdentifier;
	
	public String getCastToType(){return castToType;}
	public String getOriginIdentifier(){return originIdentifier;}
	public String getRefinedIdentifier(){return refinedIdentifier;}
	public void setCastToType(String value){castToType = value;}
	public void setOriginIdentifier(String value){originIdentifier = value;}
	public void setRefinedIdentifier(String value){refinedIdentifier = value;}
	
	@Override
	public StringBuilder change(StringBuilder outputBuffer) {
		// TODO Auto-generated method stub
		String simpleName = refinedIdentifier.split(".")[refinedIdentifier.split(".").length - 1];
		int posOfOrigin = outputBuffer.lastIndexOf(originIdentifier);
		int posOfRefined = outputBuffer.lastIndexOf(simpleName);
		if(posOfOrigin == -1) {
			int pos = posOfRefined;
			outputBuffer.insert(pos, Config.setOpen + castToType + Config.setClose);
		}else if (posOfRefined == -1){
			int pos = posOfOrigin;
			outputBuffer.insert(pos, Config.setOpen + castToType + Config.setClose);
		}else if(posOfRefined < posOfOrigin){
			int pos = posOfRefined;
			outputBuffer.insert(pos, Config.setOpen + castToType + Config.setClose);
		}else{
			int pos = posOfOrigin;
			outputBuffer.insert(pos, Config.setOpen + castToType + Config.setClose);
		}
		return outputBuffer;
	}

}
